<?php
namespace WebApper\Form\View;

class Modal extends \WebApper\Form\View {
	protected $class = "form-horizontal";

	public function render() {
		$this->_form->appendAttribute("class", $this->class);

		echo '<form', $this->_form->getAttributes(), '><fieldset>';
		$this->_form->getErrorView()->render();

		$elements = $this->_form->getElements();
		$elementSize = sizeof($elements);
		$elementCount = 0;
		$submitCount = 0;
		for($e = 0; $e < $elementSize; ++$e) {
			$element = $elements[$e];

			if ( $element instanceof \WebApper\Form\Element\ModalHeading ) {
				$id = $this->_form->getAttribute("id");
				echo '<div id="' . $id . 'Modal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="' . $id . 'ModalLabel" aria-hidden="true">
						  <div class="modal-header">
							  <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
							   ', $element->render(), '
						  </div>
						  <div class="modal-body">';
			} elseif ( $element instanceof \WebApper\Form\Element\Hidden || $element instanceof \WebApper\Form\Element\HTML || $element instanceof \WebApper\Form\Element\Legend || $element instanceof \WebApper\Form\Element\ModalToggle ) {
				$element->render();
			} elseif ( $element instanceof \WebApper\Form\Element\Button ) {
				echo '   </div>
						  <div class="modal-footer">
							  <button id="' . $id . '_close" class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
							  ', $element->render(); '
						  </div>
					  </div>';
            } else {
				echo '<div class="control-group">', $this->renderLabel($element), '<div class="controls">', $element->render(), $this->renderDescriptions($element), '</div></div>';
				++$elementCount;
			}
		}

		echo '</fieldset></form>';
    }

	protected function renderLabel(\WebApper\Form\Element $element) {
        $label = $element->getLabel();
        if(!empty($label)) {
			echo '<label class="control-label" for="', $element->getAttribute("id"), '">';
			if($element->isRequired())
				echo '<span class="required">* </span>';
			echo $label, '</label>'; 
        }
    }
}	
